<?php
/**
 * @version $Id: view.html.php 294 2011-12-20 07:04:02Z lnmtrung $
 */
 
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.view' );

class WrdViewDashboard extends WrdView{
	
	function display($tpl = null) {
		$fnc = 'display'.ucfirst($this->getLayout());
		if (is_callable(array(&$this, $fnc)))
			$this->$fnc($tpl);
		else
			parent::display($tpl);
	}

	function displayDefault($tpl = null){
		$mainframe = &JFactory::getApplication();
		
		$app = JFactory::getApplication('admin');
		$user	= &JFactory::getUser();
		
		WrdHelper::addSubMenu('dashboard');
		$this->sidebar = JHtmlSidebar::render();
	    $document = & JFactory::getDocument();
		$document->setTitle( JText::_('Dash Board') );

		JToolBarHelper::title(JText::_('Dash Board'),'activity.png');
		$product_report = $this->getListProductReport();
		
		//if($user->authorise('wrd.dashboard.admin', 'com_wrd'))
		JToolBarHelper::preferences('com_wrd','dashboard', '600');
		
		$this->assignRef('user', $user);
		$this->assignRef('product_report', $product_report);
		
	    parent::display($tpl);
  	}
  	
  	public static function getListProductReport()
  	{
  		// Initialise variables
  		$db = JFactory::getDbo();
  		$user = JFactory::getUser();
  		$query = ' SELECT * FROM #__wrd_products ORDER BY quantity ASC ';
  	
  		$db->setQuery($query, 0,10);
  		$results = $db->loadObjectList();
  	
  		// Check for database errors
  		if ($error = $db->getErrorMsg()) {
  			JError::raiseError(500, $error);
  			return false;
  		};
  	
  		foreach($results as $k => $result)
  		{
  			$arrName = unserialize($result->name);
  				
  			$results[$k]->productname = $arrName['en-GB'];
  			$results[$k]->logoutLink = '';
  			$results[$k]->editLink = JRoute::_('index.php?option=com_wrd&controller=products&layout=form&id='.$result->id);
  				
  			$results[$k]->catLink = JRoute::_('index.php?option=com_login&task=logout&uid='.$result->cat_id);
  		}
  		return $results;
  	}
}
?>